What is big O notation
big o notation is used to analyze the efficiency of an algorithm: as the size of the input growth, how drastically do the space or time requirements grow with it.
So when evaluating an algorithm efficiency we must taking to consideration the efficiency of each step within the algorithm. Then find the step that has the worst performance and prioritize it over all of the better performing steps.
$O(1)$
A constant is any step that doesn’t scale with the input to the function.
$O(n^2)$ / $O(n^3)$
1 | function Square(n) { |
$O(log n)$
1 | function logFunc(n) { |
The input n is always divided by 2, so the complexity is $O(log_{2}n)$. So if n is always divided a constant number x, it will be $O(log_{x}n)$
Binary Search
Searching must be an ordered array Thinking we need to find a certain number in this array.
- find the midpoint of our array
- figure out if the number we’re searching for is grater than or less than midpoint
- if it’s greater than our number, the left array is the right side of the midpoint. Otherwise the right array
- recur the step 1 to 3 in the left array.
1 | function BinarySearch(arr, target, start, end) { |
$O(nlog n)$
1 | function nLogNFunc(n) { |
top loop $O(log2n)$. Inner loop keep the origin value of n so it’s $O(n)$. Totally, it’s $O(nlog_{2}n)$
Merge Sort
1 | function MergeSort(arr) { |
Consider we have 4 length array, MergeSort() have two level $O(logn)$ . For EVERY LEVEL, Merge() touch each number in the array n times $O(n)$. So it’s $O(nlogn)$ totally

$O(2^n)$
Fibonacci
1 | function fib(n) { |
$O(n!)$
1 | function fn(n) { |
- 本文作者: 七渡浔
- 本文链接: https://kristineq.github.io/2022/10/06/BIG O NOTATION/
- 版权声明: 本博客所有文章除特别声明外,均采用 MIT 许可协议。转载请注明出处!